<template>
  <div>
    <el-button
                type="danger"
                size="mini"
                icon="el-icon-menu"
                @click="assignPromission()"
                >角色授权</el-button
              >
    <el-tree
      :data="treeNodes"
      show-checkbox
      node-key="id"
      ref="tree"
      check-strictly=true
      :default-expand-all=true
      :default-checked-keys="defaultChecked"
      :props="defaultProps"
    >
    </el-tree>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rid: this.$route.query.rid,
      treeNodes: [],
      defaultChecked:[],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  methods: {
    assignPromission(){
        var checkNodes = this.$refs.tree.getCheckedKeys()
        this.$axios.post("/permissions",{roleId:this.rid,pids:checkNodes})
        .then(res=>{
            if(res.data.code==200){
                this.$message.success(res.data.message);
            }else{
                this.$message.error(res.data.message);
            }            
        })
    },
    loadTreeNodes(){
        this.$axios.get("/permissions/"+this.rid)
        .then(res=>{
            this.treeNodes = res.data.data.treeNodes;
            this.defaultChecked = res.data.data.defaultChecked;
        })        
    }
  },
  mounted() {
    this.loadTreeNodes();
  },
};
</script>